local FightInterface = require("gameLogic.fightSkill.sequence.FightInterface")
local FightSkillEffect = class("FightSkillEffect", FightInterface)
local GameUtils = require("utils.GameUtils")
local FightHelper = require("gameLogic.fight.FightHelper")

function FightSkillEffect:ctor(config, targets, actor)
    self._targets = targets
    self._config = config
    self._actor = actor
    FightSkillEffect.super.ctor(self)
end

function FightSkillEffect:_update()
    if self._sequence then
        return
    end
    self:_effectPlay(self._targets)
end

-- 技能特效类型 begin
function FightSkillEffect:_effectPlay(targets)
    local targetActors = {}
    for i = 1, #targets do
        table.insert(targetActors, targets[i].actor)
    end
    local typeName = self._config.effect_type
    local effectFunc = self["_effect_" .. typeName]
    if effectFunc and type(effectFunc) == "function" then
        effectFunc(self, targetActors)
    else
        self._isDone = true
    end
end

function FightSkillEffect:_effect_single(targets)
    local FightHelper = require("gameLogic.fight.FightHelper")
    local targetPos = FightHelper.getActorCenterPos(targets)
    self:_playEffect(targetPos)
end

function FightSkillEffect:_effect_multiple(targets)
    for i, v in ipairs(targets) do
        local effectPosition = v.transform.position
        effectPosition.z = effectPosition.z + 0.3
        self:_playEffect(effectPosition)
    end
end

function FightSkillEffect:_effect_rayLine(targets)
    for i = 1, #targets do
        self:_playRayLine(targets[i])
    end
end

function FightSkillEffect:_playRayLine(tarActor)
    local GameUtils = require("utils.GameUtils")
    local Color = require("utils.Color")
    local gameObject = GameUtils.InstantiateEmptyObject("effectName")
    gameObject.transform.position = self._actor.transform.position
    local line = gameObject:AddComponent(typeof(CS.UnityEngine.LineRenderer))
    line.startColor = Color.RED_COLOR
    line.endColor = Color.RED_COLOR
    line.sharedMaterial = G_ResourceManager:loadMaterial("LineMat")
    line.positionCount = 2
    line.startWidth = 0.2
    line.endWidth = 0.2
    local startPos = self._actor.transform.position
    local endPos = tarActor.transform.position
    startPos.y = startPos.y + 1
    endPos.y = endPos.y + 1
    line:SetPosition(0, startPos)
    line:SetPosition(1, endPos)
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(self._config.effect_time)
    sequence:AppendCallback(
        function()
            self._isDone = true
            GameUtils.destroy(gameObject)
        end
    )
    self._sequence = sequence
end

function FightSkillEffect:_playEffect(effectPosition)
    local effectName = self._config.effect
    if effectName == "none" then
        self._isDone = true
        return
    end
    local object = G_ResourceManager:loadEffect(effectName)
    local gameObject = GameUtils.instantiate(object)
    gameObject.transform.position = effectPosition
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(self._config.effect_time)
    sequence:AppendCallback(
        function()
            self._isDone = true
            GameUtils.destroy(gameObject)
        end
    )
    self._sequence = sequence
end

return FightSkillEffect
